package com.itheima.service.impl;

import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    OrderSettingDao orderSettingDao;

    @Override
    public List<Map<String, Object>> getOrderSettingByMonth(String date) {
        List<Map<String, Object>> result = new ArrayList<>();
        String start = date + "-1";
        String end = date + "-31";
        Map<String, String> map = new HashMap<>();
        map.put("start", start);
        map.put("end", end);
        List<OrderSetting> orderSettings = orderSettingDao.getOrderSettingByMonth(map);
        if (orderSettings != null && orderSettings.size() > 0) {
            for (OrderSetting orderSetting : orderSettings) {
                Map<String, Object> orderSettingMap = new HashMap<>();
//                date: 1, number: 120, reservations: 1
                orderSettingMap.put("date", orderSetting.getOrderDate().getDate());
                orderSettingMap.put("number", orderSetting.getNumber());
                orderSettingMap.put("reservations", orderSetting.getReservations());
                result.add(orderSettingMap);
            }
        }
        return result;
    }

    @Override
    public void batchAddition(List<String[]> readExcel) {
        for (String[] strings : readExcel) {
            OrderSetting orderSetting = new OrderSetting();
            String orderDate = strings[0];
            Date date = new Date(orderDate);
            int number =Integer.parseInt(strings[1]) ;
            orderSetting.setOrderDate(date);
            orderSetting.setNumber(number);
            int count = orderSettingDao.findByOrderDate(date);
            if (count>0){
                orderSettingDao.editOrderDate(orderSetting);
            }else {
                orderSettingDao.addOrderDate(orderSetting);
            }
        }
    }

    @Override
    public void editOrderSettingNumber(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        int count = orderSettingDao.findByOrderDate(orderDate);
        if (count>0){
            orderSettingDao.editOrderDate(orderSetting);
        }else {
            orderSettingDao.addOrderDate(orderSetting);
        }
    }
}
